import { fileURLToPath, URL } from 'node:url'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import process from 'node:process'  
// vant和ElementPlus引入
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { VantResolver } from '@vant/auto-import-resolver'

export default ({ mode }) => {
  const env = loadEnv(mode, process.cwd(), '')
  return defineConfig({
    base: env.VITE_BASE_ROUTER,
    plugins: [
      vue(),
      vueJsx(),
      AutoImport({
        resolvers: [VantResolver(), ElementPlusResolver()]
      }),
      Components({
        resolvers: [VantResolver(), ElementPlusResolver()]
      })
    ],
    build: {
      minify: 'terser',// 必须开启：使用terserOptions才有效果
      terserOptions: {
        compress: {
          drop_console: false,
          drop_debugger: true
        }
      },
    },
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
        '@src': fileURLToPath(new URL('./src', import.meta.url)),
        '@imgs': fileURLToPath(new URL('./src/assets/imgs', import.meta.url)),
        '@css': fileURLToPath(new URL('./src/assets/css', import.meta.url)),
        '@api': fileURLToPath(new URL('./src/request/api', import.meta.url)),
        '@utils': fileURLToPath(new URL('./src/utils', import.meta.url)),
        '@comp': fileURLToPath(new URL('./src/components', import.meta.url)),
       
      },
      //引入文件的时候，可以忽略掉以下文件后缀
      extensions: ['.js', '.mjs', '.vue', '.json', '.less', '.css', '.png', '.jpg']
    },
    // 配置前端服务地址和端口
    server: {
      host: '10.58.1.54',
      port: 8997,
      // 设置反向代理，跨域
      proxy: {
        '/devApi': {
          // 前端应用和后端 API 服务器没有运行在同一个主机上，你需要在开发环境下将 API 请求代理到 API 服务器代理，
          // target: 'http://10.58.1.55:8080',
          target: 'https://gdinfo.ruiinsurance.com',
          rewrite: (path) => path.replace(/^\/devApi/, '')
        }
      }
    }
  })
}
